package com.yqy.wordvideodemo.ui;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.shizhefei.view.indicator.FixedIndicatorView;
import com.shizhefei.view.indicator.IndicatorViewPager;
import com.shizhefei.view.viewpager.SViewPager;
import com.yqy.wordvideodemo.IMainCallback;
import com.yqy.wordvideodemo.IOnBackPressed;
import com.yqy.wordvideodemo.IndicatorBean;
import com.yqy.wordvideodemo.OnTransitionTextListenerCopy3;
import com.yqy.wordvideodemo.R;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements IMainCallback {

    private SViewPager viewPager;
    private FixedIndicatorView indicator;

    private IndicatorPagerAdapter indicatorPagerAdapter;
    private IndicatorViewPager indicatorViewPager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager = findViewById(R.id.viewPager);
        indicator = findViewById(R.id.indicator);

        setIndicatorViewPagerInfo();
    }

    private void setIndicatorViewPagerInfo() {
        List<IndicatorBean> tabViewPagers = new ArrayList<>();

        tabViewPagers.add(new IndicatorBean("WORD",
                R.drawable.ic_main_home_select, R.drawable.ic_main_home_normal,
                new WordFragment("ceshi.docx")));

        tabViewPagers.add(new IndicatorBean("WORD2",
                R.drawable.ic_main_home_select, R.drawable.ic_main_home_normal,
                new WordFragment("ceshi(1).docx")));

        tabViewPagers.add(new IndicatorBean("VIDEO",
                R.drawable.ic_main_department_select, R.drawable.ic_main_department_normal,
                new VideoFragment()));

        tabViewPagers.add(new IndicatorBean("VIDEO2",
                R.drawable.ic_main_department_select, R.drawable.ic_main_department_normal,
                new VideoFragment()));

        // 禁止viewpager的滑动事件
        viewPager.setCanScroll(false);
        // 设置viewpager保留界面不重新加载的页面数量
        viewPager.setOffscreenPageLimit(tabViewPagers.size());

        indicatorPagerAdapter = new IndicatorPagerAdapter(this, getSupportFragmentManager()
                , tabViewPagers);

        indicator.setOnTransitionListener(
                new OnTransitionTextListenerCopy3()
                        .setColor(ContextCompat.getColor(this, R.color.colorTextBlack),
                                ContextCompat.getColor(this, R.color.colorTextGray))
                        .setImage(indicatorPagerAdapter.getTabIcons(), indicatorPagerAdapter.getTabUnIcons()));

        indicatorViewPager = new IndicatorViewPager(indicator, viewPager);
        indicatorViewPager.setAdapter(indicatorPagerAdapter);
    }

    @Override
    public void onBackPressed() {

        FragmentManager fragmentManager = getSupportFragmentManager();
        for (Fragment fragment :
                fragmentManager.getFragments()) {
            if (fragment.getClass().getSimpleName().equals("VideoFragment")) {
                if (((IOnBackPressed) fragment).onBackPressed()){
                    return;
                }
            }
        }
        Log.d("多少哈哈", "多少哈哈");
        super.onBackPressed();

    }

    @Override
    public void setVis(boolean vis) {
        if (vis){
            indicator.setVisibility(View.VISIBLE);
        }else {
            indicator.setVisibility(View.GONE);
        }
    }

    private class IndicatorPagerAdapter extends IndicatorViewPager.IndicatorFragmentPagerAdapter {

        private Context context;
        private LayoutInflater inflater;
        private List<IndicatorBean> mData;

        public void setNewData(List<IndicatorBean> mData) {
            this.mData = mData;
            notifyDataSetChanged();
        }

        public List<Integer> getTabIcons() {
            List<Integer> list = new ArrayList<>();
            for (IndicatorBean tabViewPagerData : mData) {
                list.add(tabViewPagerData.tabIcon);
            }
            return list;
        }

        public List<Integer> getTabUnIcons() {
            List<Integer> list = new ArrayList<>();
            for (IndicatorBean tabViewPagerData : mData) {
                list.add(tabViewPagerData.tabUnIcon);
            }
            return list;
        }

        public IndicatorPagerAdapter(Context context, FragmentManager fragmentManager) {
            super(fragmentManager);
            this.context = context;
            inflater = LayoutInflater.from(MainActivity.this);
        }

        public IndicatorPagerAdapter(Context context, FragmentManager fragmentManager, List<IndicatorBean> mData) {
            this(context, fragmentManager);
            this.mData = mData;
        }

        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public View getViewForTab(int position, View convertView, ViewGroup container) {
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.item_red_message_bottom, container, false);
            }
            TextView textView = convertView.findViewById(R.id.text);
            TextView red = convertView.findViewById(R.id.red);

            textView.setText(mData.get(position).tabName);
            textView.setCompoundDrawablesWithIntrinsicBounds(0, mData.get(position).tabIcon, 0, 0);
            red.setVisibility(View.INVISIBLE);
            return convertView;
        }

        @Override
        public Fragment getFragmentForPage(int position) {
            return mData.get(position).fragment;
        }
    }
}
